package com.bdth.weget.home.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bdth.weget.home.entity.*;
import com.bdth.weget.home.service.CarsService;
import com.bdth.weget.home.service.DepartService;
import com.bdth.weget.home.service.FleetService;
import com.bdth.weget.home.service.LocationService;
import com.bdth.weget.home.vo.CarStatus;
import com.bdth.weget.home.vo.ZTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-08
 */
@Controller
public class HomeController extends BaseController{

    @Autowired
    private DepartService departService;

    @Autowired
    private FleetService fleetService;

    @Autowired
    private CarsService carsService;

    @Autowired
    private LocationService locationService;

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response){
        User bean = (User) request.getSession().getAttribute("loginUser");
        if(bean==null){
            return "sys/login";
        }
        if(bean.getGroupid()==0)
            return "sys/admin";
        else
            return "sys/user"+bean.getGroupid();
    }

    @RequestMapping(value = "/portal/index")
    public String home(){
        return "portal/index";
    }

    @RequestMapping(value = "/portal/map")
    public String map(){
        return "portal/map";
    }

    @RequestMapping(value = "/sys/tree")
    @ResponseBody
    public List<ZTreeNode> tree(){
        List<ZTreeNode> list = new ArrayList<>();
        EntityWrapper<Depart> departEntityWrapper = new EntityWrapper<>();
        departEntityWrapper.eq("status",1);
        List<Depart> listDepart = departService.selectList(departEntityWrapper);
        EntityWrapper<Fleet> fleetEntityWrapper = new EntityWrapper<>();
        fleetEntityWrapper.eq("status",1);
        List<Fleet> listFleet = fleetService.selectList(fleetEntityWrapper);
        EntityWrapper<Cars> carsEntityWrapper = new EntityWrapper<>();
        carsEntityWrapper.eq("status",1);
        List<Cars> listCars = carsService.selectList(carsEntityWrapper);
        for(Depart depart:listDepart){
            ZTreeNode temp = new ZTreeNode();
            temp.setId("c_"+depart.getId());
            if(depart.getPid()==0){
                temp.setpId("0");
            }else{
                temp.setpId("c_"+depart.getPid());
            }
            temp.setName(depart.getDepartname());
            temp.setOpen(true);
            temp.setIcon("/img/ztree/com.png");
            list.add(temp);
        }
        for(Fleet fleet:listFleet){
            ZTreeNode temp = new ZTreeNode();
            temp.setId("f_"+fleet.getId());
            temp.setpId("c_"+fleet.getDepartid());
            temp.setName(fleet.getName());
            temp.setOpen(false);
            temp.setIcon("/img/ztree/dep.png");
            list.add(temp);
        }
        for(Cars cars:listCars){
            ZTreeNode temp = new ZTreeNode();
            temp.setId("d_"+cars.getId());
            temp.setpId("f_"+cars.getFleetid());
            temp.setName(cars.getPlateno());
            temp.setOpen(false);
            temp.setIcon("/img/ztree/offline.png");
            temp.setSimNo(cars.getSimno());
            temp.setPlateNo(cars.getPlateno());
            list.add(temp);
        }
        return list;
    }

    @RequestMapping(value = "/sys/updateTree")
    @ResponseBody
    public List<ZTreeNode> updateTree(@RequestParam(required = false) String ids){
        List<ZTreeNode> list = new ArrayList<>();

        EntityWrapper<Cars> carsEntityWrapper = new EntityWrapper<>();
        carsEntityWrapper.eq("status",1);
        if(ids!=null && !ids.equals("")) {
            carsEntityWrapper.in("id", ids);
        }
        List<Cars> listCars = carsService.selectList(carsEntityWrapper);
        EntityWrapper<Location> locationEntityWrapper = new EntityWrapper<>();
        if(ids!=null && !ids.equals("")) {
            locationEntityWrapper.in("carid", ids);
        }
        List<Location> listLocation = locationService.selectList(locationEntityWrapper);
        Map<Integer,Location> locationMap = new HashMap<>();
        for(Location location:listLocation){
            locationMap.put(location.getCarid(),location);
        }
        for(Cars cars:listCars){
            ZTreeNode temp = new ZTreeNode();
            temp.setId("d_"+cars.getId());
            temp.setpId("f_"+cars.getFleetid());
            temp.setName(cars.getPlateno());
            temp.setOpen(false);
            Location tempLocation = locationMap.get(cars.getId());
            if(tempLocation!=null && tempLocation.getStatus()==1) {
                if(tempLocation.getSpeed()>0) {
                    if(tempLocation.getWarnning()==0) {
                        //正常行驶
                        temp.setIcon("/img/ztree/3-16.png");
                    }else{
                        //告警行驶
                        temp.setIcon("/img/ztree/4-16.png");
                    }
                }else{
                    if(tempLocation.getWarnning()==0) {
                        //正常停车
                        temp.setIcon("/img/ztree/1-16.png");
                    }else{
                        //告警停车
                        temp.setIcon("/img/ztree/2-16.png");
                    }
                }
            }else{
                //车辆离线
                temp.setIcon("/img/ztree/8-16.png");
            }
            temp.setSimNo(cars.getSimno());
            list.add(temp);
        }

        return list;
    }

    @RequestMapping(value = "/sys/updateRealData")
    @ResponseBody
    public List<CarStatus> updateRealData(@RequestParam(required = false) String ids){
        List<CarStatus> list = locationService.getCarStatusList(ids);
        return list;
    }

    @RequestMapping(value = "/sys/findByDevid")
    @ResponseBody
    public Location findByDevid(@RequestParam Integer devid){
        EntityWrapper<Location> locationEntityWrapper = new EntityWrapper<>();
        locationEntityWrapper.eq("carid", devid);
        Location location = locationService.selectOne(locationEntityWrapper);
        return location;
    }
}
